import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import '../service/service_method.dart';
import '../model/category.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/child_cate.dart';
import '../routers/application.dart';


EventBus eventBus = new EventBus();

class Pass {
  String id1;
  Pass(this.id1);
}

class Catepage extends StatefulWidget {
  @override
  _CatepageState createState() => _CatepageState();
}

class _CatepageState extends State<Catepage> {
  // StreamSubscription<Pass>sss;
  String idone = "";
  @override
  void initState() {
    super.initState();
    print('2222222222222222222222222222222222222222222');
    //     sss = EventBusUtil.getInstance().on<Pass>().listen((event){
    //     print('${event.id1}');
    //     idone = event.id1;
    // });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品分类'),
      ),
      body: Container(
        child: Row(
          children: <Widget>[LeftNav(), Twoclassification()],
        ),
      ),
    );
  }
}

//左侧类别导航
class LeftNav extends StatefulWidget {
  @override
  _LeftNavState createState() => _LeftNavState();
}

class _LeftNavState extends State<LeftNav> {
  // StreamSubscription<Pass> sss;
  List list = [];
  var listIndex = 0;

  @override
  void initState() {
    super.initState();
    _getCatgort();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(200),
      decoration: BoxDecoration(
          border: Border(right: BorderSide(width: 1, color: Colors.black12))),
      child: ListView.builder(
        itemCount: list.length,
        itemBuilder: (context, index) {
          return _leftInkWell(index);
        },
      ),
    );
  }

  Widget _leftInkWell(int index) {
    bool isChilk = false;
    isChilk = (index == listIndex) ? true : false;
    return InkWell(
      onTap: () {
        setState(() {
          listIndex = index;
        });
        var childList = list[index].sub;
        Provide.value<Childcate>(context).getChildcate(childList);
        print('你点击了${list[index].cateName}分类');
        eventBus.fire(Pass('${list[index].cateId}'));
      },
      child: Container(
        height: ScreenUtil().setHeight(100),
        padding: EdgeInsets.only(left: 10, top: 20),
        decoration: BoxDecoration(
            color: isChilk ? Colors.black26 : Colors.white,
            border:
                Border(bottom: BorderSide(width: 1, color: Colors.black12))),
        child: Text(list[index].cateName),
      ),
    );
  }

  void _getCatgort() async {
    await DioUtil.request('pgetCategoryData').then((val) {
      var datas = val;
      AutogeneratedList catelist = AutogeneratedList.fromJson(datas);
      // list.data.forEach((item) => print(item.cateName));
      setState(() {
        list = catelist.data;
      });
      Provide.value<Childcate>(context).getChildcate(list[0].sub);
    });
  }
}

class Twoclassification extends StatefulWidget {
  @override
  _TwoclassificationState createState() => _TwoclassificationState();
}

class _TwoclassificationState extends State<Twoclassification> {
  // StreamSubscription<Pass> sss;
  //List list = ['啊啊','宝宝','传处','订单','煾嗯','方法','刚刚','好好','哦哦','品牌','请求','说说','天天','解决','零零'];
  String id1 = "240";
  @override
  void initState() {
    super.initState();
    eventBus.on<Pass>().listen((event) {
      print('${event.id1}');
      id1 = event.id1;
    });
  }

  Widget build(BuildContext context) {
    return Provide<Childcate>(builder: (context, child, childcate) {
      return Container(
        width: ScreenUtil().setWidth(550),
        padding: EdgeInsets.all(20.0),
        child: GridView.count(
          crossAxisCount: 3,
          children: childcate.childList.map((item) {
            return _ringhtInWell(item);
          }).toList(),
        ),
      );
    });
  }

  Widget _ringhtInWell(Sub item) {
    return InkWell(
      onTap: () {
        Application.router.navigateTo(context, 'ification?id1=${id1}&&id2=${item.cateId}');
      },
      child: Container(
          width: ScreenUtil().setWidth(152.0),
          padding: EdgeInsets.only(top: 15.0),
          child: Column(
            children: <Widget>[
              Image.network(
                item.brandImg,
                width: ScreenUtil().setWidth(152.0),
                height: ScreenUtil().setHeight(52.0),
              ),
              Text(
                item.cateName,
                style: TextStyle(fontSize: ScreenUtil().setSp(28)),
                textAlign: TextAlign.center,
              ),
            ],
          )),
    );
  }
}
